package net
{
	import flash3.net.ByteArrayPlus;
	import flash3.net.MsgPacket;
	
	import model.*;
	
	import vo.*;

	public class FrameGroup extends MessageGroup
	{
		public static const  MDM_GF_FRAME:uint				=	301								//框架消息
		
		public static const  SUB_GF_INFO:uint					=	1								//游戏信息
		public static const  SUB_GF_USER_READY:uint			=	2								//用户同意
		public static const  SUB_GF_LOOKON_CONTROL:uint		=	3								//旁观控制
		public static const  SUB_GF_KICK_TABLE_USER:uint		=	4								//踢走用户
		
		public static const  SUB_GF_OPTION:uint				=	100								//游戏配置
		public static const  SUB_GF_SCENE:uint				=	101								//场景信息
		public static const  SUB_GF_USER_CHAT:uint			=	200								//用户聊天
		public static const  SUB_GF_MESSAGE:uint				=	300								//系统消息
		public static const  SUB_GF_GIFT:uint					=	400								//赠送消息
			
		public static const  SUB_GF_LOADTIME:uint				=	500								//赠送消息
			
			
		//游戏状态
		public static const  GS_FREE:uint							=0								//空闲状态
		public static const  GS_PLAYING:uint						=100								//游戏状态
		//游戏状态
		public static const  GS_WK_FREE:uint					=GS_FREE								//等待开始
		public static const  GS_WK_SCORE:uint				=GS_PLAYING							//叫分状态
		public static const  GS_WK_DOUBLE_SCORE:uint			=GS_PLAYING+1						//加倍状态
		public static const  GS_WK_PLAYING:uint				=GS_PLAYING+2						//游戏进行
		
		public function FrameGroup()
		{
			super();
		}
		
		override public function defMessage():void
		{
			addMessage(new MessageDef(MDM_GF_FRAME, SUB_GF_INFO, "游戏信息", onGameInfo));
			addMessage(new MessageDef(MDM_GF_FRAME, SUB_GF_USER_READY, "用户准备", onUserReady));
			addMessage(new MessageDef(MDM_GF_FRAME, SUB_GF_LOOKON_CONTROL, "旁观控制", onUserLookonControl));
			addMessage(new MessageDef(MDM_GF_FRAME, SUB_GF_KICK_TABLE_USER, "踢走用户", onKickTableuser));
			addMessage(new MessageDef(MDM_GF_FRAME, SUB_GF_OPTION, "游戏配置", onOption));
			addMessage(new MessageDef(MDM_GF_FRAME, SUB_GF_SCENE, "场景信息", onScene));
			addMessage(new MessageDef(MDM_GF_FRAME, SUB_GF_USER_CHAT, "用户聊天", onUserChat));
			addMessage(new MessageDef(MDM_GF_FRAME, SUB_GF_MESSAGE, "系统消息", onMessage));
			addMessage(new MessageDef(MDM_GF_FRAME, SUB_GF_GIFT, "赠送消息", onGift));
			addMessage(new MessageDef(MDM_GF_FRAME, SUB_GF_LOADTIME, "登陆时间", onLoadTime));
		}
		
		public function onGameInfo(packet:MsgPacket):void
		{
			var bap:ByteArrayPlus = packet._data;
			bap.writeBoolean(packet._parm as Boolean);
		}
		public function onUserReady(packet:MsgPacket):void
		{
			
		}
		public function onUserLookonControl(packet:MsgPacket):void
		{
			
		}
		public function onKickTableuser(packet:MsgPacket):void
		{
			
		}
		public function onOption(packet:MsgPacket):void
		{
			var bap:ByteArrayPlus = packet._data;
	
			GameModel.i.gameStatus = bap.readByte();
			GameModel.i.allowLookon = bap.readBoolean();
		}
		public function onScene(packet:MsgPacket):void
		{
			var bap:ByteArrayPlus = packet._data;
			
			var i:uint = 0; var j:uint = 0;
			switch(GameModel.i.gameStatus)
			{
				case GS_WK_FREE:	//空闲状态
				{
					trace("空闲状态");
					
					GameModel.i.baseScore = bap.readInt();
					readBrightData(bap);
				}break;
				case GS_WK_SCORE:	//叫分状态
				{
					trace("叫分状态");
					
//					GameModel.i.preLandScore = bap.readByte();
//					GameModel.i.baseScore = bap.readInt();
//					GameModel.i.curUser  = bap.readShort();
//					
//					for(i = 0; i < 3; i++)
//						GameModel.i.gamePlayers[i].landScore = bap.readByte();
//					
//					readCardData(bap);
//					
//					readTrusteeData(bap);
//					
//					var curScorePhase:int = GameModel.i.curScorePhase = bap.readByte();
//					
//					GameModel.i.curBrightTime = bap.readByte();
//					
//					readBrightData(bap);
				}break;
				case GS_WK_DOUBLE_SCORE:	//加倍状态
				{
					trace("加倍状态");
					
//					GameModel.i.preLandScore = bap.readShort();
//					GameModel.i.baseScore = bap.readInt();
//					
//					readBackData(bap);
//					
//					readCardData(bap);
//					
//					readTrusteeData(bap);
//					
//					GameModel.i.allowDouble = bap.readBoolean();
//					
//					for(i = 0; i < 3; i++)
//						GameModel.i.gamePlayers[i].isDouble = bap.readBoolean();
//					
//					readBrightData(bap);
				}
				case GS_WK_PLAYING:		//游戏状态
				{
					trace("游戏状态");
					
					GameModel.i.preUser = bap.readShort();
					GameModel.i.bombTime = bap.readShort();
					GameModel.i.baseScore = bap.readInt();
					GameModel.i.preLandScore = bap.readByte();
					GameModel.i.outCardUser = bap.readShort();
					GameModel.i.curUser = bap.readShort();
					
					readBackData(bap);
					
					readCardData(bap);
					
					GameModel.i.outCardData.length = 0;
					var outCardCount:uint = bap.readByte();
					for(i = 0; i < outCardCount; i++)
						GameModel.i.outCardData.push(bap.readByte());
					
					readTrusteeData(bap);
					
					GameModel.i.curBrightTime = bap.readByte();
					
					readBrightData(bap);
				}break;
			}
		}
		public function onUserChat(packet:MsgPacket):void
		{
			var bap:ByteArrayPlus = packet._data;
			var userChat:UserChat = null;
			if(packet._type == MsgPacket.MSG_SEND)
			{
				userChat = packet._parm as UserChat;
				var bpl:ByteArrayPlus = new ByteArrayPlus();
				bpl.writeString({value:userChat.szChatMessage},true);
				bap.writeShort(bpl.length);
				bap.writeUnsignedInt(userChat.crFontColor);
				bap.writeUnsignedInt(userChat.dwSendUserID);
				bap.writeUnsignedInt(userChat.dwTargetUserID);
				bap.writeString({value:userChat.szChatMessage},true);
			}
			else
			{
				userChat = new UserChat();
				userChat.wChatLength = bap.readShort();
				userChat.crFontColor = bap.readUnsignedInt();
				userChat.dwSendUserID = bap.readUnsignedInt();
				userChat.dwTargetUserID = bap.readUnsignedInt();
				userChat.szChatMessage = bap.readMultiByte(userChat.wChatLength, "utf-8");
				packet._parm = userChat;
			}
			
			
		}
		public function onMessage(packet:MsgPacket):void
		{
			var bap:ByteArrayPlus = packet._data;
			
			var message:Message = new Message();
			message.wMessageType = bap.readShort();
			var lenght:uint = bap.readShort();
			message.szContent = bap.readUTFBytes(lenght);
			
			packet._parm = message;
		}
		public function onGift(packet:MsgPacket):void
		{
			
		}
		
		public function onLoadTime(packet:MsgPacket):void
		{
			
		}
		
		public function readCardData(bap:ByteArrayPlus):void
		{
			for(var i:uint = 0; i < 3; i++)
			{
				var length:uint = bap.readByte();
				GameModel.i.gamePlayers[i].cardData.length = 0;
				for(var j:uint = 0; j< length; j++)
					GameModel.i.gamePlayers[i].cardData[j] = bap.readByte();
			}
		}
		public function readBackData(bap:ByteArrayPlus):void
		{
			GameModel.i.backCard.length = 0;
			for(var i:uint = 0; i < 3; i++)
				GameModel.i.backCard.push(bap.readByte());
		}
		
		public function readTrusteeData(bap:ByteArrayPlus):void
		{
			for(var i:uint = 0; i < 3; i++)
				var isTrustee:Boolean = GameModel.i.gamePlayers[i].isTrustee = bap.readBoolean();
		}
		public function readBrightData(bap:ByteArrayPlus):void
		{
			for(var i:uint = 0; i < 3; i++)
				var isBright:Boolean = GameModel.i.gamePlayers[i].isBright = bap.readBoolean();
		}
	}
}